#include "Menu_Ctrl.h"

/*文件内部使用的宏*/

/*静态全局变量*/
static volatile uint8_t menu_idx = 0;
static volatile uint8_t menu_tick = 0;

/*全局变量*/
volatile uint8_t machine_rest_flag = MACHINE_OPEN;//机器状态 MACHINE_OPEN--开机 MACHINE_CLOSE--关机
extern volatile bool nightled_flag;//WS2812B灯带LED呼吸灯打开标志
extern volatile uint8_t led_switch;//控制LED灯带开关
extern volatile uint8_t led_ctrl_idx;//控制WS2812B的色彩
extern volatile uint8_t WS2812B_brightness;//控制WS2812B的亮度
extern volatile uint8_t water_switch;//雾化器开关标志 0--关闭 1--开启
extern char temp[10];//温度
extern char humi[10];//湿度
extern int ppb;//甲醛气体浓度值
extern float pm25;//pm2.5浓度
extern volatile uint8_t oled_display;
extern volatile uint8_t manual_open_atomizer_flag;//雾化器自动侦测标志  
                                                  //0--非手动 1--手动 2--自动
extern volatile uint8_t atomizer_minute_idx;
extern volatile uint8_t fan_switch;//风扇开关标志 0--关闭 1--开启
extern volatile uint8_t manual_open_fan_flag;//风扇自动侦测标志 
                                             //0--自动侦测关闭 1--自动侦测开启 2--自动侦测暂时关闭10分钟后开启
extern volatile uint8_t fan_minute_idx;


/*局部函数*/
static void menu_update(void)
{
    //static uint8_t flag = 0;
    char print_data[32];

    if(oled_malloc() == false)
    {
        //Serial.printf("oled_malloc error");
        return ;
    }

    if(menu_idx != 10)
    {
        if(Get_Wifi_State())//判断现在的WiFi状态
        {
            //Serial.println("STA connect");
            oled_show_pic(4, 0, 24, 24, menustatus2424[0], 0);//联网 
        }
        else
        {
            oled_show_pic(4, 0, 24, 24, menustatus2424[9], 0);//不联网
        }

        switch(oled_display)
        {
        case ICON_REMOVE:
        {
            oled_clear_ram(36, 0, 32, 64);
            break;
        }

        case ICON_AP_MODE://Smartconfig配网模式
        {
            oled_show_pic(68, 0, 24, 24, menustatus2424[11], 0);
            break;
        }

        default:
        {
            break;
        }
        }

        OLED_DrawLine(0, 28, 4);
    }

    if(menu_idx != 10)
    {
        if(Get_Aliyun_State())
        {
            oled_show_pic(36, 0, 24, 24, menustatus2424[13], 0);//连上阿里云物联网平台
        }
        else
        {
            oled_show_pic(36, 0, 24, 24, menustatus2424[12], 0);//没连上阿里云物联网平台
        }
    }

    switch(menu_idx)
    {

    case 0://显示温度
    {
        //Serial.println(temp);
        sprintf(print_data, ":%s", temp);

        oled_show_pic(0, 32, 24, 24, menustatus2424[16], 0);//显示'H'
        oled_show_string(24, 32, print_data, 24, 0);
        oled_refresh();
        break;
    }

    case 1://WS2812B灯光
    {
        oled_show_pic(0, 32, 32, 32, menu3232[0], 0);
        sprintf(print_data,"%2d", led_ctrl_idx);

        oled_show_string(32, 32, (const char *)print_data, 24, 0);
        oled_refresh();
        
        break;
    }

    case 2://雾化器
    {
        oled_show_pic(0, 32, 32, 32, menu3232[1], 0);
        sprintf(print_data, "%2d", water_switch);
        oled_show_string(32, 32, (const char *)print_data, 24, 0);
        oled_refresh();
        
        break;
    }

    case 3://雾化器自动侦测模式开关
    {
        sprintf(print_data, "%2d", manual_open_atomizer_flag);
        
        oled_show_pic(0, 32, 24, 24, menustatus2424[19], 0);//显示 设置
        oled_show_string(32, 32, (const char *)print_data, 24, 0);
        oled_refresh();
        
        break;
    }

    case 4://风扇
    {
        sprintf(print_data, "%2d", fan_switch);


        oled_show_pic(0, 32, 24, 24, menustatus2424[20], 0);//风扇 设置
        oled_show_string(32, 32, (const char *)print_data, 24, 0);
        oled_refresh();
        
        break;
    }

    case 5://风扇自动侦测功能开关
    {
        sprintf(print_data, "%2d", manual_open_fan_flag);

        oled_show_pic(0, 32, 24, 24, menustatus2424[19], 0);//显示 设置
        oled_show_string(32, 32, (const char *)print_data, 24, 0);
        
        oled_refresh();
        
        break;
    }

    case 6://显示湿度
    {
        //Serial.println(humi);
        sprintf(print_data, ":%s",humi);

        oled_show_pic(0, 32, 24,24, menustatus2424[15], 0);//显示'T'
        oled_show_string(24, 32, (const char *)print_data, 24, 0);
        oled_refresh();
        
        break;
    }

    case 7://显示甲醛气体浓度值
    {
        sprintf(print_data, ":%d",ppb);

        oled_show_pic(0, 32, 24, 24, menustatus2424[17], 0);//显示'甲醛'

        oled_show_string(24,32, (const char *)print_data, 24, 0);
        oled_refresh();
        
        break;
    }
    
    case 8://显示空气粉尘浓度
    {
        sprintf(print_data, ":%.1f",pm25);
        //Serial.println(print_data);
        
        oled_show_pic(0, 32, 24, 24, menustatus2424[18], 0);//显示'空气粉尘'

        oled_show_string(24, 32, (const char *)print_data, 24, 0);
        oled_refresh();
        break;
    }
   
    default:
    {
        break;
    }
    }

    //oled_refresh();

    oled_free();
}

/*全局函数*/
void menu_proc(uint8_t event)
{
    static uint8_t led_temp = 0;//按键1长按关闭或开启WS2812B
    static uint8_t five_second_flag = 0;
    
    switch(event)
    {
    case MENU_EVENT_TICK:
    {
        if(machine_rest_flag != MACHINE_CLOSE)
        {
            
            if((menu_idx > 0) && (menu_idx < 6))
            {
                if(menu_tick > 0)
                {
                    menu_tick--;
                    break;
                }
                else
                {
                    menu_idx = 0;
                }
            }

            five_second_flag++;

            if(five_second_flag == 5)
            {
                if(menu_idx == 0)
                {
                    menu_idx = 6;
                }
                else if(menu_idx == 6)
                {
                    menu_idx = 7;
                }
                else if(menu_idx == 7)
                {
                    menu_idx = 8;
                }
                else if(menu_idx == 8)
                {
                    menu_idx = 0;
                }
                five_second_flag = 0;
            }

            menu_update();
        }
        break;
    }
    case MENU_EVENT_BUTTON1:
    {
        if(machine_rest_flag != MACHINE_CLOSE)
        {
            nightled_flag = false;//关呼吸灯
            menu_tick = 2;
            // if(menu_idx == 4)
            // {
            //     break;
            // }
            menu_idx = 1;//WS2812B灯光
            led_ctrl_idx++;
            if(led_ctrl_idx > 8)
            {
                led_ctrl_idx = 1;
            }
            
            led_temp = 0;

            Update_WS2812B_Brightness_To_Aliyun(WS2812B_brightness);

            switch(led_ctrl_idx)
            {
            case 1:
            {
                WS2812B_SetGRB(120, 10, 0);//红橙色
                Update_WS2812B_Color_To_Aliyun(1);
                //Serial.println("0 color");
                break;
            }

            case 2:
            {
                WS2812B_SetGRB(70, 150, 255);//蓝白色
                Update_WS2812B_Color_To_Aliyun(2);
                //Serial.println("1 color");
                break;
            }
            
            case 3:
            {
                WS2812B_SetGRB(80, 40, 240);//紫白色
                Update_WS2812B_Color_To_Aliyun(3);
                //Serial.println("2 color");
                break;
            }

            case 4:
            {
                WS2812B_SetGRB(0, 100, 50);//青色
                Update_WS2812B_Color_To_Aliyun(4);
                //Serial.println("3 color");
                break;
            }

            case 5:
            {
                WS2812B_SetGRB(90, 0, 30);//玫瑰色
                Update_WS2812B_Color_To_Aliyun(5);
                //Serial.println("4 color");
                break;
            }

            case 6:
            {
                WS2812B_SetGRB(30, 60, 100);
                Update_WS2812B_Color_To_Aliyun(6);
                //Serial.println("5 color");
                break;
            }

            case 7:
            {
                WS2812B_SetGRB(160, 80, 0);//黄色
                Update_WS2812B_Color_To_Aliyun(7);
                //Serial.println("6 color");
                break;
            }

            case 8:
            {
                nightled_flag = true;
                Update_WS2812B_Color_To_Aliyun(8);
                break;
            }

            default:
            {
                led_ctrl_idx = 0;
                break;
            }

            }
            menu_update();
        }
        break;
    }
    
    case MENU_EVENT_LONG_BUTTON1:
    {
        if(machine_rest_flag != MACHINE_CLOSE)
        {
            nightled_flag = false;//关呼吸灯
            menu_tick = 2;
            // if(menu_idx == 4)
            // {
            //     break;
            // }
            menu_idx = 1;//WS2812B灯光
            if(led_temp == 0)
            {
                //WS2812B_SetGRB(0, 0, 0);
                WS2812B_Close();
                Update_WS2812B_Color_To_Aliyun(0);
                led_temp = 1;
                //led_ctrl_idx = 0;
            }
            else
            {
                led_temp = 0;
                switch(led_ctrl_idx)
                {
                case 1:
                {
                    WS2812B_SetGRB(120, 10, 0);
                    Update_WS2812B_Color_To_Aliyun(1);
                    //Serial.println("0 color");
                    break;
                }

                case 2:
                {
                    WS2812B_SetGRB(70, 150, 255);
                    Update_WS2812B_Color_To_Aliyun(2);
                    //Serial.println("1 color");
                    break;
                }

                case 3:
                {
                    WS2812B_SetGRB(80, 40, 240);
                    Update_WS2812B_Color_To_Aliyun(3);
                    //Serial.println("2 color");
                    break;
                }

                case 4:
                {
                    WS2812B_SetGRB(0, 100, 50);
                    Update_WS2812B_Color_To_Aliyun(4);
                    //Serial.println("3 color");
                    break;
                }

                case 5:
                {
                    WS2812B_SetGRB(90, 0, 30);
                    Update_WS2812B_Color_To_Aliyun(5);
                    //Serial.println("4 color");
                    break;
                }

                case 6:
                {
                    WS2812B_SetGRB(30, 60, 100);
                    Update_WS2812B_Color_To_Aliyun(6);
                    //Serial.println("5 color");
                    break;
                }

                case 7:
                {
                    WS2812B_SetGRB(160, 80, 0);
                    Update_WS2812B_Color_To_Aliyun(7);
                    //Serial.println("6 color");
                    break;
                }

                case 8:
                {
                    //WS2812B_Close();
                    //Update_WS2812B_Color_To_Aliyun(0);
                    //led_ctrl_idx = 0;
                    nightled_flag = true;
                    Update_WS2812B_Color_To_Aliyun(8);
                    break;
                }

                default:
                {
                    break;
                }

                }
            }
            menu_update();
        }
        break;
    }

    case MENU_EVENT_TWO_BUTTON1:
    {
        switch(led_switch)
        {
        case 0:
        {
            led_switch = 1;
            Led_Open();
            Update_LED_Switch_To_Aliyun(1);
            break;
        }
        
        case 1:
        {
            led_switch = 0;
            Led_Close();
            Update_LED_Switch_To_Aliyun(0);
            break;
        }
        }
        break;
    }

    case MENU_EVENT_BUTTON2:
    {
        if(machine_rest_flag != MACHINE_CLOSE)
        {
            menu_tick = 2;
            menu_idx = 2;//雾化器

            if(manual_open_atomizer_flag == 1)//雾化器自动侦测开启
            {
                manual_open_atomizer_flag = 2;//自动侦测暂时关闭 10分钟后再开启
                Update_Atomizer_Manual_Flag_To_Aliyun(manual_open_atomizer_flag);
            }
            else if(manual_open_atomizer_flag == 2)
            {
                atomizer_minute_idx = 0;
            }

            if(water_switch == 0)
            {
                Atomizer_Open();
                water_switch = 1;
            }
            else
            {
                Atomizer_Close();
                water_switch = 0;
            }


            Update_Humidified_To_Aliyun(water_switch);
            menu_update();
        }
        break;
    }

    case MENU_EVENT_LONG_BUTTON2:
    {
        if(machine_rest_flag == MACHINE_CLOSE)
        {
            //Serial.println("machine restart");
            oled_clear();//清空屏幕
            ESP.restart();//重启
        }
        else if(machine_rest_flag == MACHINE_OPEN)
        {
            machine_rest_flag = MACHINE_CLOSE;
            //Serial.println("machine off");
            OLED_Off();//关闭OLED屏幕
            nightled_flag = false;//关闭呼吸灯
            Atomizer_Close();//关闭雾化器
            Fan_Close();//关闭风扇
            WS2812B_Close();//关闭灯带
            Update_WS2812B_Color_To_Aliyun(0);//更新WS2812B的颜色状态
            Update_Humidified_To_Aliyun(0);//更新雾化器为关闭
            Update_WS2812B_Brightness_To_Aliyun(0);//更新WS2812B的亮度
            Update_FanSwitch_To_Aliyun(0);//更新风扇为关闭
            Update_LED_Switch_To_Aliyun(0);//更新LED灯为关闭
            Disconnect_To_Aliyun();//断开阿里云物联网平台的连接
            Serial1.end();//断开ZE08CH2O
            Serial2.end();//断开ZPH02
            WiFi.disconnect();//断开WiFi
        }
        break;
    }

    case MENU_EVENT_TWO_BUTTON2:
    {
        if(machine_rest_flag != MACHINE_CLOSE)
        {
            menu_tick = 2;
            menu_idx = 3;//雾化器侦测模式开关
            //manual_open_atomizer_flag = 0;
            if(manual_open_atomizer_flag == 1)//如果雾化器自动侦测模式开启
            {
                manual_open_atomizer_flag = 0;//则关闭雾化器自动侦测模式
            }
            else if(manual_open_atomizer_flag == 0 || manual_open_atomizer_flag == 2)//如果雾化器自动侦测模式关闭 或 雾化器自动侦测模式暂时关闭
            {
                manual_open_atomizer_flag = 1;//则开启雾化器自动侦测模式
            }
            
            Update_Atomizer_Manual_Flag_To_Aliyun(manual_open_atomizer_flag);
            menu_update();
        }
        break;
    }

    case MENU_EVENT_BUTTON3:
    {
        if(machine_rest_flag != MACHINE_CLOSE)
        {
            menu_tick = 2;
            menu_idx = 4;//风扇

            if(manual_open_fan_flag == 1)
            {
                manual_open_fan_flag = 2;
                Update_Fan_Manual_Flag_To_Aliyun(manual_open_fan_flag);
            }
            else if(manual_open_fan_flag == 2)
            {
               fan_minute_idx = 0; 
            }

            if(fan_switch == 0)
            {
                Fan_Open();
                fan_switch = 1; 
            }
            else 
            {
                Fan_Close();
                fan_switch = 0;
            }

            Update_FanSwitch_To_Aliyun(fan_switch);
            menu_update();
        }
        break;
    }

    case MENU_EVENT_LONG_BUTTON3:
    {
        if(machine_rest_flag != MACHINE_CLOSE)
        {
            menu_tick = 2;
            menu_idx = 5;//风扇自动侦测开关

            if(manual_open_fan_flag == 1)//如果风扇自动侦测模式开启
            {
                manual_open_fan_flag = 0;//则关闭风扇自动侦测模式
            }
            else if( (manual_open_fan_flag == 0) ||  (manual_open_fan_flag == 2) )//如果风扇自动侦测模式关闭 或 风扇自动侦测模式暂时关闭
            {
                manual_open_fan_flag = 1;//则开启风扇自动侦测模式
            }

            Update_Fan_Manual_Flag_To_Aliyun(manual_open_fan_flag);
            menu_update();
        }
        break;
    }
    
    case MENU_EVENT_TWO_BUTTON3://open smartconfig
    {
        if(machine_rest_flag != MACHINE_CLOSE)
        {
            if(Get_Wifi_Mode() == 0)
            {
                WiFiMode_Set(0);//进入smartconfig模式
            }
            else
            {
                WiFiMode_Set(1);//退出smartconfig模式
            }
        }
        break;
    }
    
    case MENU_EVENT_NULL:
    {
        break;
    }

    default:
    {
        break;
    }
    }
}